package vn.cuhanh.sys;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DBAdapter {

	// Database fields
	private String table ;
	private Context context;
	private SQLiteDatabase database;
	private DBHelper dbHelper;

	public DBAdapter(Context context, String tableName) {
		this.context = context;
		this.table = tableName;
	}

	public DBAdapter open() throws SQLException {
		dbHelper = new DBHelper(context);
		database = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		if (dbHelper != null)
			dbHelper.close();
		dbHelper = null;
	}
	public boolean isClosed(){
		return (dbHelper == null);
	}
	/** 
	 * insert a new games If the games is successfully download  
	 * return the new rowId for that note, otherwise return a -1 to indicate failure. 
	 */
	public long insert(ContentValues values) {
//		ContentValues values = new ContentValues();
//		values.put("_id", id);
//		values.put("name", name);
//		values.put("sourceName", sourceName);
//		values.put("packageName", packageName);
//		values.put("activity", activity);
//		values.put("img", img);
//		values.put("rate", rate);
//		values.put("numDownload", numDownload);
//		Log.d("database", "database " + String.valueOf(database == null) + " table " + String.valueOf(this.table == null) + " value " + String.valueOf(values == null));
		return database.insert(this.table, null, values);
	}

	/** 
	 * Update the games 
	 */
	public boolean update(ContentValues values, String where) {
//		ContentValues values = new ContentValues();
//		values.put("_id", id);
//		values.put("name", name);
//		values.put("sourceName", sourceName);
//		values.put("packageName", packageName);
//		values.put("activity", activity);
//		values.put("img", img);
//		values.put("rate", rate);
//		values.put("numDownload", numDownload);
		
//		return database.update(table, values, " _id = " + id, null) > 0;
		return database.update(this.table, values, where, null) > 0;
	}

	/** 
	 * Deletes games 
	 */
	public boolean delete(String where) {
//		return database.delete(this.Table, "_id =" + rowId, null) > 0;
		return database.delete(this.table, where, null) > 0;
	}
	
	/**
	 * Return a Cursor over the list of all games in the database 
	 * @return Cursor over all notes 
	 */
	public Cursor fetchAll() {
		Cursor mCursor =  database.query(this.table, null, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	/** 
	 * Return a Cursor positioned at the defined games 
	 */
	public Cursor fetch(String where) throws SQLException {
		Cursor mCursor = database.query(true, this.table, null, where, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	
	public boolean deleteAll() {
		return database.delete(this.table, null, null) > 0;
	}
	
	public void drop(){
		database.execSQL("DROP TABLE IF EXISTS " + this.table);
	}
	
	/** 
	 * Return a Cursor 
	 */
	public Cursor query(String sql ) throws SQLException {
		Cursor mCursor =  database.rawQuery(sql, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	
//	public void createTable()
//	{
//		String[] query = this.context.getResources().getStringArray(R.array.sql_create);
//		for (int i = 0; i < query.length; i++)
//		{
//			database.execSQL(query[i]);
//			Log.v("..........create table", query[i]);
//		}
//	}
}